package Unique

import (
	"bytes"
	"gitee.com/yebetter/dingding_robot/helper/library/snowFlake"
	"strconv"
)

var nodeId int64 = 1

/**
通过雪花算法生成唯一id
*/
func GetUniqueNum() (string, error) {
	worker, err := snowFlake.NewWorker(nodeId)
	if err != nil {
		return "0", err
	}
	id := worker.GetId()
	idStr := strconv.FormatInt(id, 10)
	return idStr, nil
}

/**
通过雪花算法生成可定义前缀的唯一id
1-开屏 2-吉利视频 3-插屏 5-banner 6-信息流 7-视频流 8-jpush
*/
func GetUniqueNumPrex(prex string) (string, error) {
	worker, err := snowFlake.NewWorker(nodeId)
	if err != nil {
		return "0", err
	}
	id := worker.GetId()
	idStr := strconv.FormatInt(id, 10)
	var ad bytes.Buffer
	ad.WriteString(idStr)
	ad.WriteString(prex)
	str := ad.String()
	ad.Reset()
	return str, nil
}
